﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using DcsGauges.Server.Models;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;

namespace DcsGauges.Server.ViewModels
{
    class ServerViewModel : ViewModelBase
    {
        #region Fields / Properties

        private ServerModel                             model               = null;

        #endregion


        #region Notify Properties

        private string startStopServerButtonText = "Start";
        public string StartStopServerButtonText
        {
            get { return this.startStopServerButtonText; }
            set
            {
                if(this.startStopServerButtonText.Equals(value) == false)
                {
                    this.startStopServerButtonText = value;
                    this.OnPropertyChanged(MethodBase.GetCurrentMethod());
                }
            }
        }

        private string gameConnectStatus              = "None connected";
        public string GameConnectStatus
        {
            get { return this.gameConnectStatus; }
            set
            {
                if(this.gameConnectStatus != value)
                {
                    this.gameConnectStatus = value;
                    this.OnPropertyChanged(MethodBase.GetCurrentMethod());
                }
            }
        }

        private BitmapSource connectedClientLogo = default(BitmapSource);
        public BitmapSource ConnectedClientLogo
        {
            get { return this.connectedClientLogo; }
            set
            {
                if(this.connectedClientLogo != value)
                {
                    this.connectedClientLogo = value;
                    this.OnPropertyChanged(MethodBase.GetCurrentMethod());
                }
            }
        }

        private string serverStatus = "Stopped";
        public string ServerStatus
        {
            get { return this.serverStatus; }
            set
            {
                if(this.serverStatus.Equals(value) == false)
                {
                    this.serverStatus = value;
                    this.OnPropertyChanged(MethodBase.GetCurrentMethod());
                }
            }
        }

        #endregion

        #region Commands

        private ICommand startStopServerCommand                 = null;
        public ICommand StartStopServerCommand
        {
            get
            {
                if(this.startStopServerCommand == null)
                {
                    this.startStopServerCommand = new DelegateCommand<object>((o) => 
                        { 
                            if(this.model.IsServerRunning == true)
                            {
                                this.model.Stop();
                            }
                            else
                            {
                                this.model.Start();
                            }
                        });
                }

                return this.startStopServerCommand;
            }
        }
    
        #endregion



        public ServerViewModel(ServerModel model)
        {
            this.model                              = model;
            this.model.ServerModelChangedEvent      += OnServerModelChangedEvent;

            this.StartStopServerCommand.Execute(null);
        }

        void OnServerModelChangedEvent(object sender, EventArgs e)
        {
            this.GameConnectStatus              = string.Format(Localization.ConnectedGameClient1A, this.model.ConnectedGameClient);
            this.ConnectedClientLogo            = this.model.ConnectedGameClientLogo;
            this.StartStopServerButtonText      = this.model.IsServerRunning ? "Stop" : "Start";
            this.ServerStatus                   = this.model.IsServerRunning ? "Running" : "Stopped";
        }
    }
}
